﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rated.DAL.Db;
using Rated.Website.Helpers;
using Rated.Website.Exceptions;

namespace Rated.Website.Models
{
    public class LocationViewModel
    {
        public int ContinentID { get; set; }
        public int CountryID { get; set; }
        public int LocalityID { get; set; }
        public int StateID { get; set; }
        public int CityID { get; set; }
        public int ItemID { get; set; }

        public LocationViewModel()
        {
            throw new EIncorrectConstructor();
        }

        public LocationViewModel(int itemId)
        {
            var db = new RatedEntities();
            RatedItem item = db.RatedItems.Where(o => o.ID == itemId).Single();
            ContinentID = item.ContinentID ?? 0;
            CountryID = item.CountryID ?? 0;
            LocalityID = item.LocalityID ?? 0;
            StateID = item.StateID ?? 0;
            CityID = item.CityID ?? 0;
            ItemID = itemId;
        }

        public LocationViewModel(System.Collections.Specialized.NameValueCollection values)
        {
            ItemID = int.Parse(values["ItemID"]);
            var db = new RatedEntities();
            RatedItem item = db.RatedItems.Where(o => o.ID == ItemID).Single();

            ContinentID = values["ContinentID"] == null ? 0 : int.Parse(values["ContinentID"]);
            CountryID = values["CountryID"] == null ? 0 : int.Parse(values["CountryID"]);
            LocalityID = values["LocalityID"] == null ? 0 : int.Parse(values["LocalityID"]);
            StateID = values["StateID"] == null ? 0 : int.Parse(values["StateID"]);
            CityID = values["CityID"] == null ? 0 : int.Parse(values["CityID"]);

            item.ContinentID = ContinentID == 0 ? null : (int?)ContinentID;
            item.CountryID = CountryID == 0 ? null : (int?)CountryID;
            item.LocalityID = LocalityID == 0 ? null : (int?)LocalityID;
            item.StateID = StateID == 0 ? null : (int?)StateID;
            item.CityID = CityID == 0 ? null : (int?)CityID;
            db.SaveChanges();

        }
    }
}